package com.neuedu.brazil;

import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.apache.hadoop.io.WritableComparable;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;

/**
 * Hadoop自定义数据类型
 *
 * @author DEll
 */
@Getter
@Setter
@NoArgsConstructor
public class WeatherWritable implements WritableComparable<WeatherWritable> {
    /**
     * 气象站代码
     */
    private String code;
    /**
     * 日期
     */
    private String date;
    /**
     * 降水量
     */
    private double precipitation;
    /**
     * 气温
     */
    private double maxTemperature;
    private double minTemperature;
    private double avgTemperature;

    public WeatherWritable(String code, String date) {
        this.code = code;
        this.date = date;
    }

    public WeatherWritable(String code, String date, double precipitation, double maxTemperature, double minTemperature, double avgTemperature) {
        this(code, date);
        this.precipitation = precipitation;
        this.maxTemperature = maxTemperature;
        this.minTemperature = minTemperature;
        this.avgTemperature = avgTemperature;
    }

    @Override
    public String toString() {
        return code + '\t' +
                date + '\t' +
                precipitation + '\t' +
                maxTemperature + '\t' +
                minTemperature + '\t' +
                avgTemperature;
    }

    @Override
    public int compareTo(WeatherWritable other) {
        if (null == other) {
            return 1;
        }
        return this.date.compareTo(other.date);
    }

    @Override
    public void write(DataOutput out) throws IOException {
        out.writeUTF(this.code);
        out.writeUTF(this.date);
        out.writeDouble(this.precipitation);
        out.writeDouble(this.maxTemperature);
        out.writeDouble(this.minTemperature);
        out.writeDouble(this.avgTemperature);

    }

    @Override
    public void readFields(DataInput in) throws IOException {
        this.code = in.readUTF();
        this.date = in.readUTF();
        this.precipitation = in.readDouble();
        this.maxTemperature = in.readDouble();
        this.minTemperature = in.readDouble();
        this.avgTemperature = in.readDouble();

    }

}
